/*
* AirController.java
* Created on  202015/6/26 10:22
* �汾       �޸�ʱ��          ����      �޸�����
* V1.0.1    202015/6/26       longshihui    ��ʼ�汾
*
*/

package com.infitecs.eyas.enthalpy;

import com.infitecs.eyas.enthalpy.model.Air;
import com.infitecs.eyas.enthalpy.service.AirService;
import com.infitecs.eyas.session.model.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/enthalpy/air")
public class AirController {
    @Autowired
    private AirService airService;

    @RequestMapping("")
    public String getPage(HttpSession session) {
        try {
            int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
            if (airService.getAir(caseId) == null) {
                airService.addAir(caseId);
            }
        } catch (Exception e) {
            return "enthalpy/air";
        }
        return "enthalpy/air";
    }

    @RequestMapping(value = "/update/{Gx}", method = RequestMethod.POST)
    @ResponseBody
    public void updateGx(@PathVariable("Gx") String name, HttpServletRequest request, HttpSession session) {
        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
        Param param = new Param(name.trim());
        param.setParamValue(Double.parseDouble(request.getParameter(name)));
        param.setCaseId(caseId);
        airService.updateGx(param);
    }

    @RequestMapping(value = "/getAir", method = RequestMethod.POST)
    @ResponseBody
    public Air getAir(HttpSession session) {
        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
        return airService.getAir(caseId);
    }

    @RequestMapping(value = "/calculate/{Gx}", method = RequestMethod.POST)
    @ResponseBody
    public Double calGx(@PathVariable("Gx") String Gx, HttpSession session) {
        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
        Param param = new Param(Gx);
        param.setCaseId(caseId);
        switch (Gx) {
            case "G8":
                return airService.calG8(param);
            case "G9":
                return airService.calG9(param);
        }
        return null;
    }
}
